package com.dk.action;

import com.alibaba.fastjson.JSON;
import com.dk.domain.ActionEntity;
import com.dk.domain.action.WaitActionBodyEntity;
import com.dk.exception.ExcelDataException;
import com.dk.util.log.Log;

import java.util.concurrent.TimeUnit;

/**
 * 用例等待动作
 */
public class CaseWait implements ActionStrategy{
    /**
     * 用例等待
     * @param actionEntity 动作实体
     * @throws ExcelDataException
     */
    @Override
    public void Action(ActionEntity actionEntity) throws ExcelDataException {
        //获取动作主体内容
        String actionBody = actionEntity.getActionBody();
        //转换为等待动作主体对象
        WaitActionBodyEntity waitActionBodyEntity = JSON.parseObject(actionBody, WaitActionBodyEntity.class);
        if(waitActionBodyEntity.getTime()>10000){
            throw new ExcelDataException("---【errorr】:等待时长不得超过10s,请检查测试数据:CaseNum:"+actionEntity.getActionNum()+";");
        }
        try {
            Log.info("---【用例等待】:开始等待，等待时长:"+waitActionBodyEntity.getTime()+"毫秒");
            //开始等待
            TimeUnit.MILLISECONDS.sleep(waitActionBodyEntity.getTime());
            Log.info("---【用例等待】:等待结束");
        } catch (InterruptedException e) {
            Log.error("---【error】:执行用例等待异常，请检查数据,CaseNum:"+actionEntity.getActionNum()+";");
        }
    }
}
